var http_request = null;
var params;

function toserver(f) {
	var rating;
	var ucomment;
	
	//gets the value of the selected radio button
  	for (var r=0; r < f.rating.length; r++){
   		if (f.rating[r].checked){
	     rating = f.rating[r].value;
     	}
   	 }

   	//gets the value of the textbox
   	 ucomment = f.comment.value ;	
	
   	//gets the url and nym from the URL
   	var url_val = getURLParam("url");
	var nym_val = getURLParam("nym");

	//set the parameters to send to the server
	params = "nym="+nym_val+"&url_val="+url_val+"&rating="+rating+"&comment="+ucomment;
	
	var url = "loadXMLDoc(\"http://silo.cs.indiana.edu:13795/servlet/ExplicitRatingServlet\");";
	setTimeout(url, 0); 

}
	
function loadXMLDoc(url) {
	
	alert(url);
	
	if (window.XMLHttpRequest){ 
		http_request = new XMLHttpRequest(); 
        http_request.onreadystatechange = alertContents();
        http_request.open('POST', url, true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(params);                      //initialize the request 
     }

      else if(window.ActiveXObject){ 
          //Create our RequestObject
          http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
          if(http_request){ 
	        http_request.onreadystatechange = function() { alertContents(http_request); };
        	http_request.open('POST', url, true);
        	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        	http_request.send("nym=home&url_val=www.google.com&rating=-2");                      //initialize the request 
        	}
          }
      
     if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
       

}

function alertContents() {

        try {
            if (http_request.readyState == 4) {
                if (http_request.status == 200) {
                   	var items = [];	
					var b1 = req.responseXML.getElementsByTagName('valid');
					var valid = b1.item(0).firstChild.nodeValue;
					valid = format(valid);
					if(valid == "1")
					{
						alert("Thank you for you input");
						window.close();
					}
					else
					{
						alert("An unexpected error has occured. Please try again later.");
					}
                } else {
                    alert('There was a problem with the request.');
                }
            }
        }
        catch( e ) {
             alert('Caught Exception: ' + e);
        }

}

//this function gets rid of leading whitespace
function format(val) {
	
	vlength = val.length;
	val = val.slice(1, vlength-1);	
	return val;
		
}

//this function gets the parameters from the url
function getURLParam(strParamName){
	var strReturn = "";
	var strHref = document.location.href;
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
			var aParam = aQueryString[iParam].split("=");
			strReturn = aParam[1];
			break;
			}
		}

	}

	return strReturn;

}
